package org.styx.db;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @date 2021/9/27
 * @auther uranus
 */
@SpringBootApplication
public class DBApplication {
    @Autowired
    private DataSource dataSource;
    public static void main(String[] args) {
        SpringApplication.run(DBApplication.class,args);
    }
    @PostConstruct
    public void init() throws SQLException {

        exec("sql/seata-server.sql");
        exec("sql/account.sql");
        exec("sql/order.sql");
        exec("sql/storage.sql");
    }

    private void exec(String s) throws SQLException{
        System.out.println(
                "----------------------开始执行-----------------------------");
        ClassPathResource resource=new ClassPathResource(s,
                DBApplication.class.getClassLoader());
        EncodedResource encodedResource=new EncodedResource(resource,"UTF-8");
        ScriptUtils.executeSqlScript(dataSource.getConnection(),encodedResource);
        System.out.println(
                "--------------------执行结束---------------------------------");

    }
}
